import 'package:flutter/material.dart';

/// create 2019-07-29 by cai

class SimpleAppBar extends StatelessWidget with PreferredSizeWidget {
  final Size size;

  final Color backgroundColor;

  final title;

  final List<Widget> actions;

  final bool centerTitle;

  final double elevation;

  final Widget leading;

  SimpleAppBar({
    Key key,
    this.size,
    this.backgroundColor,
    this.title = "",
    this.actions = const [],
    this.centerTitle = true,
    this.elevation = 0.0,
    this.leading,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    Color bgColor = backgroundColor ??
        theme.appBarTheme.color ??
        theme?.primaryColor ??
        Colors.blue;
    Widget title = createTitle();
    return AppBar(
      backgroundColor: bgColor,
      title: title,
      leading: leading,
      actions: actions,
      centerTitle: centerTitle,
      elevation: elevation,
      bottom: PreferredSize(
        child: Container(
          color: Color(0xFFF4F4F4),
          height: 1,
        ),
        preferredSize: Size.fromHeight(1),
      ),
    );
  }

  Widget createTitle() {
    if (title is String) {
      return Text(title);
    }
    if (title is Widget) {
      return title;
    }
    if (title == null) {
      return Text("");
    }
    return Text(title.toString());
  }

  @override
  Size get preferredSize => this.size ?? Size.fromHeight(48);
}
